import request from "@/utils/request";

const BASE_URL = "/api/v1/task-assignments";

const TaskAssignmentsAPI = {
  /** 获取任务矩阵 */
  getMatrix(params?: TaskMatrixQuery) {
    return request<any, any>({
      url: `${BASE_URL}/matrix`,
      method: "get",
      params,
    });
  },
  /** 发布任务（幂等） */
  assign(data: TaskAssignForm) {
    return request({
      url: `${BASE_URL}/assign`,
      method: "post",
      data,
    });
  },
  /** 编辑子项 */
  updateItem(id: number, passScore: number) {
    return request({
      url: `${BASE_URL}/item/${id}`,
      method: "put",
      params: { passScore },
    });
  },
  /** 删除子项 */
  deleteItem(id: number) {
    return request({
      url: `${BASE_URL}/item/${id}`,
      method: "delete",
    });
  },
  /** 导出当日任务明细CSV */
  exportCsv(targetDate: string) {
    return request<any, Blob>({
      url: `${BASE_URL}/export`,
      method: "get",
      params: { targetDate },
      responseType: 'blob' as any,
    })
  }
};

export default TaskAssignmentsAPI;

export interface TaskAssignForm {
  studentId: number;
  targetDate: string; // yyyy-MM-dd
  items: TaskAssignItemForm[];
}

export interface TaskAssignItemForm {
  seriesId: number;
  programId: number;
  unitId: number;
  passScore: number;
}

export interface TaskMatrixQuery extends PageQuery {
  dateStart?: string;
  dateEnd?: string;
  keyword?: string;
}
